package com.basic.nio.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @Author: wisdom
 * @Date: 2024/3/31  1:47 PM
 * @Description:
 */
public class Client {

    public static void main(String[] args) throws IOException {
        //获取通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8080));
        //非阻塞模式
        socketChannel.configureBlocking(false);
        //缓冲器容量设置
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //发送消息
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请说：");
            String msg = scanner.next();
            buffer.put(msg.getBytes());
            buffer.flip();

            socketChannel.write(buffer);
            buffer.clear();
        }

    }
}
